package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.dto.ElderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.service.IElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletResponse;

/**
 * 老人Service业务层处理
 * 
 * @author zhai
 * @date 2025-01-02
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService
{
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     * 
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id)
    {
        return elderMapper.selectById(id);
    }

    /**
     * 查询老人列表
     * 
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder)
    {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder)
    {
        return elderMapper.insert(elder);
    }

    /**
     * 修改老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder)
    {
        return elderMapper.updateById(elder);
    }

    /**
     * 批量删除老人
     * 
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids)
    {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人信息
     * 
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id)
    {
        return elderMapper.deleteById(id);
    }

    /**
     * 分页查询已经入住的老人列表
     *
     * @param elderDto 前端传递的老人信息
     * @return 返回老人列表
     */
    @Override
    public TableDataInfo<Elder> pageQuery(ElderDto elderDto) {
        //1.分页查询条件
        Page<Elder> page = new Page<>(elderDto.getPageNum(),elderDto.getPageSize());
        LambdaQueryWrapper<Elder> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.eq(Elder::getStatus, elderDto.getStatus())
                .like(StringUtils.isNotEmpty(elderDto.getName()),Elder::getName, elderDto.getName())
                .like(StringUtils.isNotEmpty(elderDto.getIdCardNo()),Elder::getIdCardNo, elderDto.getIdCardNo());
         page = page(page, wrapper);


        return getTableDateInfo(page);
    }

    /**
     * 传递查询条件
     * @param page
     * @return 返回getTableDateInfo
     */
    private TableDataInfo<Elder> getTableDateInfo(Page<Elder> page) {
        TableDataInfo<Elder> getTableDateInfo = new TableDataInfo<>();
        getTableDateInfo.setMsg("查询成功");
        getTableDateInfo.setCode(HttpServletResponse.SC_OK);
        getTableDateInfo.setTotal(page.getTotal());
        getTableDateInfo.setRows(page.getRecords());
        return getTableDateInfo;
    }


}
